let v = new Vue({
    el: '#app',
    data: {
        userId:'',
        hot:[],     //热销商品
        goods:{     //具体的商品
            id:'',
            count:1,
            userCollect:false
        },
        images:[]  //商品图
    },
    methods: {
        initGoodsDetail:function () {   //查询热销商品
            axios(`goods/initGoodsDetail/${this.goods.id}/${this.userId}`)
                .then(rsp=>{
                    if (rsp.data.flag){
                        this.hot = rsp.data.data.hot;
                        this.images = rsp.data.data.images;
                        this.goods = rsp.data.data.goods;
                    }
                })
                .catch(error=>{
                    layer.msg(error.message);
                });
        },
        toGoodsDetail:function (id) {
             localStorage.setItem("aid",id);
        },
        changeUserCollect:function (flag) {     //是否收藏/取消收藏，true为收藏，反之取消收藏
            axios.put(`goods/changeUserCollect/${this.goods.id}/${this.userId}/${flag}`)
                .then(rsp=>{
                    if (rsp.data.flag){
                        if (flag){
                            layer.msg("收藏成功");
                        }else{
                            layer.msg("取消收藏成功");
                        }
                        this.goods.userCollect = !this.goods.userCollect;
                    }
                })
                .catch(error=>{
                    layer.msg(error.message);
                });
        },
        insertToCart:function () {
            let goodsCount = document.getElementById("goods_count").value;
            if (goodsCount < 1){
                layer.msg("商品数不能小于1");
                return false;
            }
            this.goods.count = goodsCount;
            axios.post(`cart/insertToCart`,this.goods)
                .then(rsp=>{
                    if (rsp.data.flag){
                        layer.msg("添加成功");
                    }
                })
                .catch(error=>{
                    layer.msg(error.message);
                });
        }
    },
    created: function () {
        this.goods.id =  localStorage.getItem("aid");
        this.userId =  localStorage.getItem("userId");
        this.initGoodsDetail();
    }
});